package models

import (
	"time"
)

type Employment struct{
	Model
	EmployerId int `json:"employerId" gorm:"employer_id"`
	EmployeeId int `json:"employeeId" gorm:"employee_id"`
	StationId int `json:"stationId" gorm:"station_id"`
	StartTime time.Time `json:"startTime" gorm:"column:start_time;default:null"`
}

type APIEmployment struct {
	Model
	EmployerId int `json:"employerId" gorm:"employer_id"`
	EmployeeId int `json:"employeeId" gorm:"employee_id"`
	Username string `json:"username"`
	Telephone string `json:"telephone"`
	Avatar int `json:"avatar"`
	Des string `json:"des"`
	StationId int `json:"stationId" gorm:"station_id"`
	StartTime time.Time `json:"startTime" gorm:"start_time"`
}

func CheckAuthority(userId, stationId int) bool {
	var station Station
	db.Table("bms_station").Select("id").Where("id = ? and owner_id = ?", stationId, userId).First(&station)
	if station.ID > 0{
		return true
	}
	var employment Employment
	db.Table("bms_employment").Select("id").Where("station_id = ? and (employer_id = ? or employee_id = ?)", stationId, userId, userId).First(&employment)
	return employment.ID > 0
}

func ExistEmploymentById(id int) bool {
	var employment Employment
	db.Select("id").Where("id = ?", id).First(&employment)
	return employment.ID > 0
}

func ExistEmploymentByInfo(employerId, employeeId, stationId int) bool {
	var employment Employment
	db.Select("id").Where("employer_id = ? AND employee_id = ? AND station_id = ?", employerId, employeeId, stationId).First(&employment)
	return employment.ID > 0
}

func GetEmployees(employerId int) (apiEmployments []APIEmployment) {
	db.Raw("select bms_employment.id as id, bms_employment.employer_id as employer_id, bms_employment.employee_id as employee_id, bms_employment.station_id as station_id, bms_employment.start_time as start_time, bms_user.username as username, bms_user.telephone as telephone, bms_user.avatar as avatar, bms_user.des as des from bms_employment inner join bms_user on bms_employment.employee_id = bms_user.id where employer_id = ?", employerId).Scan(&apiEmployments)
	//db.Table("bms_employment").Select("bms_employment.id as id, bms_employment.employer_id as employer_id, bms_employment.employee_id as employee_id, bms_employment.station_id as station_id, bms_employment.start_time as start_time, bms_user.username as username, bms_user.telephone as telephone, bms_user.avatar as avatar, bms_user.desc as desc").Joins("inner join bms_user on bms_employment.employee_id = bms_user.id where bms_employment.employer_id = ?", employerId).Scan(&apiEmployments)
	return
}

func GetEmployers(employeeId int) (apiEmployments []APIEmployment) {
	db.Raw("select bms_employment.id as id, bms_employment.employer_id as employer_id, bms_employment.employee_id as employee_id, bms_employment.station_id as station_id, bms_employment.start_time as start_time, bms_user.username as username, bms_user.telephone as telephone, bms_user.avatar as avatar, bms_user.des as des from bms_employment inner join bms_user on bms_employment.employer_id = bms_user.id where employee_id = ?", employeeId).Scan(&apiEmployments)
	//db.Table("bms_employment").Select("bms_employment.id as id, bms_employment.employer_id as employer_id, bms_employment.employee_id as employee_id, bms_employment.station_id as station_id, bms_employment.start_time as start_time, bms_user.username as username, bms_user.telephone as telephone, bms_user.avatar as avatar, bms_user.desc as desc").Joins("inner join bms_user on bms_employment.employer_id = bms_user.id where bms_employment.employee_id = ?", employeeId).Scan(&apiEmployments)
	return
}

func GetEmploymentsForStation(stationId int) (employments []Employment) {
	db.Where("station_id = ?", stationId).Find(&employments)
	return
}

func AddEmployment(employerId, employeeId, stationId int) bool {
	db.Create(&Employment{
		EmployerId: employerId,
		EmployeeId: employeeId,
		StationId: stationId,
	})
	return true
}

func DeleteEmployment(id int) bool {
	db.Where("id = ?", id).Delete(&Employment{})
	return true
}

func UpdateEmployment(id int, data interface{}) bool {
	db.Model(&Employment{}).Where("id = ?", id).Updates(data)
	return true
}